package com.example.app.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.example.app.R;
import com.example.app.adapter.catalog.CategoryListAdapter;
import com.example.app.api.Apiservice;
import com.example.app.fragment.catalog.CatalogcontentFragment;
import com.example.app.interfaces.ResponseCallBack;
import com.example.app.viewmodel.ResponseData;
import com.example.app.viewmodel.catalog.Catalog;
import com.example.app.viewmodel.catalog.CategoryList;

import java.util.List;

public class CatalogFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    ListView listView;
    FrameLayout frameLayout;
    CategoryListAdapter categoryListAdapter;
    private List<CategoryList> categoryLists;
    CatalogcontentFragment contentFragment;

    public CatalogFragment() {
    }

    public static CatalogFragment newInstance(String param1, String param2) {
        CatalogFragment fragment = new CatalogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    public void initView(View view){
        //加载视图
        listView = view.findViewById(R.id.catalog_list);
        frameLayout = view.findViewById(R.id.catalog_content);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_catalog, container, false);

        //初始化视图
        initView(view);

        //获取数据
        Apiservice.GetCatalog(new ResponseCallBack<ResponseData<Catalog>>() {
            @Override
            public void Success(ResponseData<Catalog> responseData) {
                categoryLists = responseData.getData().getCategoryList();

                //1.分类
                categoryListAdapter = new CategoryListAdapter(view.getContext(),categoryLists);
                listView.setAdapter(categoryListAdapter);
                selectItem(0);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        selectItem(position);
                        //每一项 Fragment
                        contentFragment = CatalogcontentFragment.newInstance(String.valueOf(categoryLists.get(position).getId()));
                        getChildFragmentManager().beginTransaction()
                                .replace(R.id.catalog_content, contentFragment)
                                .commit();
                        //Log.e("TAG", "onItemClick: " + category.getId());
                    }
                });

                //默认第一项
                if (contentFragment == null) {
                    contentFragment = CatalogcontentFragment.newInstance(String.valueOf(categoryLists.get(0).getId()));
                    getChildFragmentManager().beginTransaction()
                            .replace(R.id.catalog_content, contentFragment)
                            .commit();
                }

            }
        });
        return view;
    }

    public void selectItem(int position){
        for (int i = 0; i < categoryLists.size(); i++) {
            categoryLists.get(i).setChecked(false);
            categoryLists.get(position).setChecked(true);
        }
        categoryListAdapter.notifyDataSetChanged();
    }

}